﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace EDXLSharp.NIEMCore
{
  /// <summary>
  /// A data type for a character string.
  /// </summary>
  public abstract class TextType : ComplexTypeBase
  {
    /// <summary>
    /// The content of this object
    /// </summary>
    public string Text;

    public TextType() { }
    public TextType(string text)
    {
      Text = text;
    }

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xwriter is null</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();
      
      xwriter.WriteElementString(this.ElementPrefix, this.ElementName, this.ElementNamespace, this.Text);
    }

    /// <summary>
    /// Reads this Object's values from an XML nodelist
    /// </summary>
    /// <param name="rootnode">root XML Node of the Object element</param>
    /// <exception cref="System.ArgumentNullException">Thrown if rootnode is null</exception>
    /// <exception cref="System.ArgumentException">Thrown if the rootnode is not the correct type</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }

      if (rootnode.LocalName != this.ElementName)
      {
        throw new ArgumentException("Unexpected element " + rootnode.LocalName + " instead of " + this.ElementName);
      }

      this.Text = rootnode.InnerText;

      this.Validate();
    }

    /// <summary>
    /// When overridden, validates the content against applicable rules
    /// </summary>
    public override void Validate()
    {
    }
  }
}
